package cn.itsource.org.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.digest.MD5;
import cn.itsource.basic.domain.AjaxResult;
import cn.itsource.basic.domain.BaseQuery;
import cn.itsource.basic.domain.LoginVO;
import cn.itsource.basic.domain.UserInfo;
import cn.itsource.basic.util.PageList;
import cn.itsource.basic.util.RedisService;
import cn.itsource.org.domain.Department;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.mapper.DepartmentMapper;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.org.service.EmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    DepartmentMapper departmentMapper;
    @Autowired
    EmployeeMapper employeeMapper;
    @Autowired
    ShopMapper shopMapper;
    @Autowired
    RedisService redisService;

    @Override
    public void insert(Employee employee) {
        employeeMapper.insertSelective(employee);
    }

    @Override
    public void update(Employee employee) {
        employeeMapper.updateByPrimaryKeySelective(employee);
    }

    @Override
    public PageList<Employee> findPage(BaseQuery baseQuery, String key) {

        //1.创建分页对象
        PageHelper.startPage(baseQuery.getPage(), baseQuery.getPageSize());
        PageList<Employee> page = employeeMapper.findPage(baseQuery, key);
        PageInfo<Employee> pageInfo = new PageInfo<>(page.getList());
        List<Employee> list = pageInfo.getList();
        list.forEach(employee -> {
            employee.setDepartment(departmentMapper.loadById(employee.getDepartmentId()));
            employee.setShop(shopMapper.selectByPrimaryKey(employee.getShopId()));
        });
        return new PageList<>(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public void delete(Long id) {
        employeeMapper.delete(id);
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        employeeMapper.deleteBatch(ids);
    }

    @Override
    public List<Employee> findAll() {
        return employeeMapper.findall();
    }

    @Override
    public Employee findOne(Long id) {
        return employeeMapper.findOne(id);
    }

    @Override
    public AjaxResult login(Employee employee) {
        Employee login = employeeMapper.login(employee);
        if(Objects.isNull(login)) throw  new RuntimeException("用户不存在");
        String salt = login.getSalt();
        String passwordMD5 = MD5.create().setSalt(salt.getBytes()).digestHex(employee.getPassword());
        if(!Objects.equals(login.getPassword(), passwordMD5)){
            return AjaxResult.setMsg("用户不存在");
        }
        UserInfo userInfo = new UserInfo();
        BeanUtil.copyProperties(login,userInfo);
        String uuid = IdUtil.fastSimpleUUID();
        redisService.setStringKeyAndValue(uuid, login, 30);
        return AjaxResult.setResult(new LoginVO(uuid, userInfo));
    }

    @Override
    public List<Employee> findByShopId(Long shopId) {
        return employeeMapper.findByShopId(shopId);
    }
}
